package com.travelms.service.commonuserpage;

import com.travelms.pojo.Lvyou;
import com.travelms.dao.DBFind;
import com.travelms.util.DataEffect;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

public class FindSpot {
    public FindSpot(String username) throws SQLException, IOException {
        Stack<Integer> stack = new Stack<>();
        Scanner sc = new Scanner(System.in);
        String s;//中间效验输入是否为数字
        int current = 0;//记录当前目录层级
        stack.push(current);//初始化目录层级为0
        while (true) {
            if (current == 0) {
                System.out.println("1.输入景点名查询");
                System.out.println("2.输入目的地查询");
                System.out.println("3.退出");
                while (true) {
                    System.out.println("亲输入:");
                    s = sc.nextLine();
                    if (!DataEffect.isNumber(s)) {//输入效验
                        System.out.println("输入错误");
                    } else if (!(s.compareTo("0") > 0 && s.compareTo("4") < 0)) {
                        System.out.println("输入错误");
                    } else {
                        current = Integer.parseInt(s);//记录输入的层级
                        stack.push(current);//压入栈
                        break;
                    }
                }
                if (current == 3) { //如果输入为8则退出
                    break;
                }

            }
            if (current == 1) {
                String dest = null;
                while (true) {
                    System.out.println("请输入景点名:");
                    dest = sc.nextLine();
                    if (dest.isEmpty()){
                        System.out.println("输入错误");
                    }else {
                        break;
                    }
                }
                DBFind dbFind = new DBFind();
                ArrayList<Lvyou> lvyous = dbFind.FindSpot(dest);
                if (lvyous.isEmpty()) {
                    System.out.println("现在没有该景点在售卖");
                }else {
                    Lvyou lvyou = lvyous.get(0);

                    System.out.println("查询成功");
                    System.out.println(lvyou);
                    if (lvyou.getReminder() == 0){
                        System.out.println("该景点票已经售空");
                        continue;
                    }
                    System.out.println("是否要购买该票:Y/N");
                    String is = sc.nextLine();
                    if ("y".equals(is) ||"Y".equals(is)){
                        boolean b = dbFind.buySpotTicket(lvyou, username);
                        if (b){
                            System.out.println("购买成功");
                        }else {
                            System.out.println("购买失败，检查余额是否充足");
                        }

                    }else {
                        System.out.println("没有购买");
                    }
                }

                stack.pop();
                current = stack.peek();
            }else if (current == 2){
                //按目标地址查询

                DBFind dbFind = new DBFind();
                ArrayList<Lvyou> lvyous = null;
                while (true) {
                    String dest = null;
                    while (true) {
                        System.out.println("请输入目的地,或者输入exit退出");
                        dest = sc.nextLine();
                        if (!dest.isEmpty()){
                            break;
                        }
                        System.out.println("输入错误");
                    }
                    lvyous = dbFind.FindSpot2(dest);
                    if("exit".equals(dest)){
                        break;
                    }else if (lvyous.isEmpty()){
                        System.out.println("没有该景点");
                    }else {
                        lvyous.forEach(System.out::println);
                        int start = lvyous.get(0).getId();
                        int end = lvyous.get(lvyous.size() - 1).getId();
                        while (true) {
                            String isOrExit = null;//这里
                            while (true) {
                                System.out.println("选择你要购买的票的id，或者输入exit退出");
                                isOrExit = sc.nextLine();

                                if ("exit".equals(isOrExit)) {

                                    break;
                                } else if (isOrExit.matches("\\d+")) {
                                    int inputNumber = Integer.parseInt(isOrExit);
                                    if (inputNumber >= start && inputNumber <= end) {

                                        break;
                                    } else {
                                        System.out.println("输入不在范围");
                                    }
                                } else {
                                    System.out.println("输入错误");
                                }
                            }

                            if ("exit".equals(isOrExit)){
                                break;
                            }else {
                                int  buySuccess = dbFind.isBuySuccess(isOrExit, username);
                                if (buySuccess == 1){
                                    System.out.println("票已经没有啦！！！");
                                }else if(buySuccess == 2){
                                    System.out.println("请检查余额");
                                }else {
                                    dbFind.writeSale(isOrExit,username);
                                    System.out.println("购买成功");
                                }

                            }
                            break;
                        }
                        break;
                    }
                }

                stack.pop();
                current =  stack.peek();
            }

        }
    }

}

